home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 …ember: Reference Library / Dev.CD Dec 97 RL.toast / Technical Documentation / Rhapsody_OpenStep_Docs / Release_Notes / Documents / terminal.m < prev   
Encoding:
Text File  |  1997-10-14  |  4.8 KB  |  154 lines  |  [TEXT/BOBO]

  1. //
  2. //  terminal.m
  3. //
  4. //        Sample client for Terminal.app's Distributed Objects services.
  5. //        Copyright 1994-1997, Apple Computer, Inc. All rights reserved.
  6. //
  7. //
  8. //    This program opens a terminal window in the current directory. It's installed
  9. //    in /usr/bin/terminal.
  10. //
  11. //    Author: sam streeper
  12. //    Updated by: Barry Locklear, Grant Baillie
  13. //
  14. //    To compile:
  15. //
  16. //      cc -I/NextDeveloper/Headers/Apps terminal.m -framework Foundation -o terminal
  17. //
  18. //    Usage:
  19. //    terminal [-shell] [-dir <dir>] [-noclose] [-env] [command]
  20. //
  21.  
  22. #import <AppKit/AppKit.h>
  23. #import <Foundation/Foundation.h>
  24. #import <stdio.h>
  25. #import <mach/mach.h>
  26. #import <servers/bootstrap.h>
  27. #import "TerminalDOProtocol.h"
  28.  
  29. static void usage(void) {
  30.     fprintf(stderr,"terminal: open a new Terminal.app window\n\n");
  31.     fprintf(stderr,"Usage: terminal [-shell] [-dir <dir>] [-noclose] [-env] [command]\n");
  32.     fprintf(stderr,"    -shell   - Run command from default shell\n");
  33.     fprintf(stderr,"    -dir     - Specify working directory (default is current)\n");
  34.     fprintf(stderr,"    -noclose - Retain window upon exit\n");
  35.     fprintf(stderr,"    -env     - Export current environment\n");
  36.     exit(-1);
  37. }
  38.  
  39. static id<TSTerminalDOServices> lookupTerminalDOServer(void) {
  40.     port_t sendMachPort;
  41.     NSDistantObject *rootProxy = nil;
  42.     id<TSTerminalDOServices> result;
  43.  
  44.     // First, try look up Terminal's DO object in the bootstrap server.
  45.     // This is where the app registers it by default.
  46.     if ((BOOTSTRAP_SUCCESS == bootstrap_look_up(bootstrap_port, "TerminalDO", &sendMachPort)) && (PORT_NULL != sendMachPort)) {
  47.         NSConnection *conn = [NSConnection connectionWithReceivePort:[NSPort port] sendPort:[NSPort portWithMachPort:sendMachPort]];
  48.         rootProxy = [conn rootProxy];
  49.     }
  50.  
  51.     // If the previous call failed, the following might succeed if the user
  52.     // logged in is running Terminal with the PublicDOServices user default
  53.     // set.
  54.     if (!rootProxy) {
  55.         rootProxy = [NSConnection rootProxyForConnectionWithRegisteredName:@"TerminalDO" host:@""];
  56.     }
  57.  
  58.     // We could also try to launch Terminal at this point, using
  59.     // the NSWorkspace protocol.
  60.     if (!rootProxy) {
  61.         fprintf(stderr,"Can't connect to Terminal\n");
  62.         exit(-1);
  63.     }
  64.  
  65.     [rootProxy setProtocolForProxy:@protocol(TSTerminalDOServices)];
  66.     result = (id<TSTerminalDOServices>)rootProxy;
  67.  
  68.     if ([result protocolVersion] < 0x10002) {
  69.         fprintf(stderr,"Incompatible terminal protocol version\n");
  70.         exit(-4);
  71.     }
  72.  
  73.     return result;
  74. }
  75.  
  76. int main(void) {
  77.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  78.     id <TSTerminalDOServices> terminal = lookupTerminalDOServer();
  79.  
  80.     NSEnumerator *argsEnum = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
  81.     NSString *thisArg;
  82.  
  83.     int returnCode;
  84.     NSMutableString *command = nil;
  85.     NSDictionary *environment = nil;
  86.     NSString *directory = nil;
  87.     TSShellType shellType = TSShellNone;
  88.     TSExitAction exitAction = TSCloseUnlessError;
  89.  
  90.     int winHandle;
  91.  
  92.     NSData *inputData = nil;
  93.     NSData *outputData = nil;
  94.     NSData *errorData = nil;
  95.  
  96.  
  97.     thisArg = [argsEnum nextObject]; /* Skip the program name */
  98.  
  99.     if (nil == thisArg) {
  100.         shellType = TSShellDefault;
  101.     }
  102.  
  103.     // Run through the command-line arguments. We only set command to
  104.     // something non-nil once we encounter an argument that isn't an
  105.     // option. Once we've done this, though, we treat the rest of
  106.     // the arguments as part of the command.
  107.     //
  108.     while(thisArg = [argsEnum nextObject]) {
  109.         if (command) {
  110.             [command appendFormat:@" %@", thisArg];
  111.         }
  112.         else if (![thisArg hasPrefix:@"-"]) {
  113.             command = [[thisArg mutableCopy] autorelease];
  114.         }
  115.         else  if ([@"-shell" isEqualToString:thisArg]) {
  116.             shellType = TSShellDefault;
  117.         }
  118.         else if ([@"-dir" isEqualToString:thisArg]) {
  119.             if (!(thisArg = [argsEnum nextObject])) usage();
  120.             directory = thisArg;
  121.         }
  122.         else if ([@"-noclose" isEqualToString:thisArg]) {
  123.             exitAction = TSDontCloseOnExit;
  124.         }
  125.         else if ([@"-env" isEqualToString:thisArg]) {
  126.             environment = [[NSProcessInfo processInfo] environment];
  127.         }
  128.         else {
  129.             usage();
  130.         }
  131.     }
  132.  
  133.     if (!directory) directory = [[NSFileManager defaultManager] currentDirectoryPath];
  134.     if (!command) command = [NSMutableString stringWithCString:""];
  135.  
  136.     [terminal runCommand:command
  137.         inputData:inputData
  138.         outputData:&outputData
  139.         errorData:&errorData
  140.         waitForReturn:NO
  141.         windowType:TSWindowNew
  142.         windowHandle:&winHandle
  143.         exitAction:exitAction
  144.         shellType:shellType
  145.         windowTitle:command
  146.         directory:directory
  147.         environment:environment
  148.         returnCode:&returnCode];
  149.  
  150.     [pool release];
  151.  
  152.     exit(returnCode);
  153. }
  154.